package com.shiwaixiangcun.customer.ui.activity


import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.GlobalConfig
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.SurroundMerchAdapter
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.MerchantListBean
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.shiwaixiangcun.customer.utils.JsonUtil.fromJson
import com.shiwaixiangcun.customer.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.activity_surround_top.*
import kotlinx.android.synthetic.main.top_bar_white.*
import java.util.*

/**
 * @author Administrator
 */
class SurroundTopActivity : BaseActivity(), View.OnClickListener {
    private var mDataList = ArrayList<MerchantListBean.DataBean.ElementsBean>()
    private var mSurroundMerchAdapter: SurroundMerchAdapter? = null
    private var merchant = ""
    private var nameType: String? = null
    var mPageSize = GlobalConfig.page_size
    var mCurrentPage = GlobalConfig.first_page

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_surround_top)
        initBundleData()

        initViewAndEvent()
        initData(false)
    }

    /**
     * 获取BundleData
     */
    private fun initBundleData() {
        val intent = intent

        merchant = intent.getStringExtra("merchant")
        nameType = intent.getStringExtra("nameType")
    }

    private fun initViewAndEvent() {

        if (nameType?.isNotEmpty()!!) {
            tv_page_name?.text = nameType
        } else {
            tv_page_name?.text = ""
        }
        back_left.setOnClickListener(this)
        mSurroundMerchAdapter = SurroundMerchAdapter(mDataList)

        rv_type_list.adapter = mSurroundMerchAdapter
        rv_type_list.layoutManager = LinearLayoutManager(this)


        mSurroundMerchAdapter?.setOnItemClickListener { adapter, view, position ->
            val item = adapter.data[position] as MerchantListBean.DataBean.ElementsBean
            val bundle = Bundle()
            bundle.putString("type", item.typeValue)
            bundle.putString("merchId", item.id.toString() + "")
            readyGo(SurroundWebActivity::class.java, bundle)

        }

        refreshLayout.setOnRefreshListener({
            mCurrentPage = 1
            initData(false)
            mSurroundMerchAdapter?.setEnableLoadMore(false)
        })
        mSurroundMerchAdapter?.setLoadMoreView(CustomLoadMoreView())
        mSurroundMerchAdapter?.setOnLoadMoreListener({
            mCurrentPage += 1
            initData(true)
        }, rv_type_list)


    }

    private fun initData(isLoadMore: Boolean) {

        val siteId = AppSharePreferenceMgr.get(mContext, GlobalConfig.CURRENT_SITE_ID, 20) as Int

        OkGo.get<String>(GlobalApi.merchant)
                .params("fields", "cover,id,name,feature,typeValue")
                .params("typeId", merchant)
                .params("siteId", siteId)
                .params("page.pn", mCurrentPage)
                .params("page.size", mPageSize)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {

                        val fromJson = fromJson<MerchantListBean>(response?.body(), MerchantListBean::class.java) ?: return
                        if (fromJson.responseCode == 1001) {
                            val size = fromJson.data.elements.size
                            if (isLoadMore) {

                                if (size > 0) {
                                    mDataList.addAll(fromJson.data.elements)
                                    mSurroundMerchAdapter?.notifyDataSetChanged()
                                }

                                if (size == 0 || size < mPageSize) {
                                    mSurroundMerchAdapter?.loadMoreEnd(false)
                                } else {
                                    mSurroundMerchAdapter?.loadMoreComplete()
                                }
                            } else {
                                refreshLayout.finishRefresh()
                                mSurroundMerchAdapter?.setEnableLoadMore(true)
                                when {
                                    size == 0 -> {
                                    }
                                    else -> {
                                        mDataList.clear()
                                        mDataList.addAll(fromJson.data.elements)
                                        mSurroundMerchAdapter?.notifyDataSetChanged()
                                    }
                                }
                            }
                        } else {
                            showToastShort("获取数据出错")
                        }
                    }
                })

    }

    override fun onClick(view: View) {
        val id = view.id
        when (id) {
            R.id.back_left -> finish()
        }
    }


}
